/**
 * Created by Trialiet on 2016/9/21.
 */
Date.prototype.Format = function(fmt)
{
    var o = {
        "M+" : this.getMonth()+1,                 //月份
        "d+" : this.getDate(),                    //日
        "h+" : this.getHours(),                   //小时
        "m+" : this.getMinutes(),                 //分
        "s+" : this.getSeconds(),                 //秒
        "q+" : Math.floor((this.getMonth()+3)/3), //季度
        "S"  : this.getMilliseconds()             //毫秒
    };
    if(/(y+)/.test(fmt))
        fmt=fmt.replace(RegExp.$1, (this.getFullYear()+"").substr(4 - RegExp.$1.length));
    for(var k in o)
        if(new RegExp("("+ k +")").test(fmt))
            fmt = fmt.replace(RegExp.$1, (RegExp.$1.length==1) ? (o[k]) : (("00"+ o[k]).substr((""+ o[k]).length)));
    return fmt;
}

$(document).ready(function () {
    var date = new Date().Format("yyyy-MM-dd");
    $("#start-date").val(date);
    $("#end-date").val(date);
});

var orderPager = {
    "currentPage" : 1,
    "maxPages" : 0,
    "pageRow" : 10,
    "data" : null
};

function getDailyTurnover() {
    var startDate = $("#start-date").val() + " 00:00:00";
    var endDate = $("#end-date").val() + " 23:59:59";
    var json = JSON.stringify(orderPager);
    $.ajax({
        type: "post",
        url: "dailyTurnover.action",
        data: "{\"orderPager\":" + json + ", \"startDate\":\"" + startDate + "\", \"endDate\": \"" + endDate + "\"}",
        contentType: "application/json;charset=utf-8",
        dataType: 'json',
        async: true,
        success: function (data) {
            var pages = data.orderPager.maxPages;
            orderPager.maxPages = pages;
            if(data.orderPager.data.length == 0){
                $("#page-div").append("<div class='alert alert-warning'>" +
                    "<a href='#' class='close' data-dismiss='alert'>&times;" +
                    "</a>没有查询到数据。</div>");
            }
            if (pages > 1){
                $("#pager").append("<li><a href='#' id='last'>&laquo;</a></li>");
                for(var i = 1; i < pages+1; i++){
                    $("#pager").append("<li><a href='#' onclick='onPage(" + i + ")'>" + i +"</a></li>");
                }
                $("#pager").append("<li><a href='#' id='next'>&raquo;</a></li>");
            }
            var list = data.orderPager.data;
            $.each(list, function (key, value) {
                show(value);
            })
        }
    });
}

function show(data) {
    $("#turnover").append("<tr><td>"+data.time+"</td>"
    + "<td>"+data.receive+"</td>" 
    + "<td>"+ data.zhaoling +"</td></tr>"
    );
}

function onPage(pageNo) {
    orderPager.currentPage = pageNo;
    var json = JSON.stringify(orderPager);
    $.ajax({
        type: "post",
        url: "dailyTurnover.action",
        data: "{\"orderPager\":" + json + "}",
        contentType: "application/json;charset=utf-8",
        dataType: 'json',
        async: true,
        success: function (data) {
            $("#turnover").html("");
            $.each(data.orderPager.data, function (key, value) {
                show(value);
            })
        }
    });
}

